# 拉取 CentOS7
FROM hub.c.163.com/library/centos:latest

# 维护者
MAINTAINER regan <850535450@qq.com>

# 设置 PHP 和redis版本
ENV PHP_VERSION 7.2.12
ENV REDIS_VERSION 5.0.2
ENV SWOOLE_VERSION 4.4.3
ENV MONGO_VERSION 1.6.0

ENV PHP_EXTEND_DIRECTORY '/usr/local/src'

ADD php-7.2.12.tar.gz $PHP_EXTEND_DIRECTORY

ADD redis5.0.2.tar.gz $PHP_EXTEND_DIRECTORY

ADD swoole4.4.3.tar.gz $PHP_EXTEND_DIRECTORY

ADD mongodb-1.6.0.tgz $PHP_EXTEND_DIRECTORY

# 安装依赖
RUN set -x \
  && yum update -y \
  && yum install epel-release -y \
  && yum install -y wget git gcc gcc-c++ make automake autoconf perl file tar re2c libjpeg libpng libjpeg-devel libpng-devel libjpeg-turbo freetype freetype-devel \
        libcurl-devel libxml2-devel libjpeg-turbo-devel libXpm-devel libXpm libicu-devel libmcrypt libmcrypt-devel libxslt-devel libxslt openssl openssl-devel bzip2-devel libzip \

  && mkdir ~/download \

  && cd $PHP_EXTEND_DIRECTORY \

  # 编译 PHP
  && cd php-$PHP_VERSION \
  && groupadd -r nginx \
  && useradd -r -g nginx nginx \
  && ./configure \
       --prefix=/usr/local/php \
       --with-config-file-path=/usr/local/php/etc/ \
       --with-config-file-scan-dir=/usr/local/php/conf.d/ \
       --enable-fpm \
       --enable-cgi \
       --with-fpm-user=nginx  \
       --with-fpm-group=nginx \
       --disable-phpdbg \
       --enable-mbstring \
       --enable-calendar \
       --with-xsl \
       --with-openssl \
       --enable-soap \
       --enable-zip \
       --enable-shmop \
       --enable-sockets \
       --with-gd \
       --with-freetype-dir=/usr/include/freetype2/freetype \
       --with-jpeg-dir \
       --with-png-dir \
       --with-xpm-dir \
       --with-xmlrpc \
       --enable-pcntl \
       --enable-intl \
       --with-mcrypt \
       --enable-sysvsem \
       --enable-sysvshm \
       --enable-sysvmsg \
       --enable-opcache \
       --with-iconv \
       --with-bz2 \
       --with-curl \
       --enable-mysqlnd \
       --with-mysqli=mysqlnd \
       --with-pdo-mysql=mysqlnd \
       --with-zlib \
       --with-gettext \
  && make \
  && make install \

  # 复制配置文件
  && cp $PHP_EXTEND_DIRECTORY/php-$PHP_VERSION/php.ini-production /usr/local/php/etc/php.ini \
  && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
  && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \


  # 安装 Redis 扩展
  && cd $PHP_EXTEND_DIRECTORY \
  && cd phpredis-$REDIS_VERSION \
  && /usr/local/php/bin/phpize \
  && ./configure --with-php-config=/usr/local/php/bin/php-config \
  && make \
  && make install \

  # 加入环境变量
  && echo "export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH" >> ~/.bashrc \
  && source ~/.bashrc \

  # 安装 Composer
  && cd ~/download \
  && php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
  && php composer-setup.php --install-dir=/usr/local/sbin --filename=composer \
  && php -r "unlink('composer-setup.php');" \

  # 安装swoole 扩展
  && cd $PHP_EXTEND_DIRECTORY \
  && cd swoole-src-$SWOOLE_VERSION \
  && /usr/local/php/bin/phpize \
  && ./configure --enable-openssl \
  && make \
  && make install \

  # 安装mongo 扩展
  && cd $PHP_EXTEND_DIRECTORY \
  && cd mongodb-$MONGO_VERSION \
  && /usr/local/php/bin/phpize \
  && ./configure --with-php-config=/usr/local/php/bin/php-config \
  && make \
  && make install \

  # 删除安装文件
  && rm -rf ~/download \
  && yum clean all

CMD ["/usr/local/php/sbin/php-fpm"]

EXPOSE 9000